package cn.edu.scau.cmi.wujiaming.experiment5.consistent;

import java.util.ArrayList;
import java.util.List;

public class ConsistentComposite extends ConsistentComponent {
	private String name;
	private List<ConsistentComponent> list = new ArrayList<>();
	
	public ConsistentComposite(String name) {
		this.name = name;
	}
	@Override
	public String getName() {
		return "文件夹" + name;
	}
	
	@Override
	public String add(ConsistentComponent cc) {
		if(getFather() == null) {
			setFather(this);
		}
		cc.setFather(getFather());
		list.add(cc);
		return name + "添加" + cc.getName()  + "成功！！！";
	}
	@Override
	public String remove(ConsistentComponent cc) {
		if (list.contains(cc)) {
			if (cc.getList() == null || cc.getList().size() == 0)
				cc.setFather(null);
			else 
				cc.setFather(cc);
			list.remove(cc);
			if (list.size() == 0)
				setFather(null);
			return name + "删除" + cc.getName()  + "成功！！！";
		} else {
			return name + "删除" + cc.getName()  + "失败！！！";
		}
	}
	@Override
	public List<ConsistentComponent> getList() {
		return list;
	}
	@Override
	public String doSomething(boolean display, String tab) {
		StringBuilder content = new StringBuilder();
		if (display)
			content.append(tab + getName() + "\n");
		else 
			content.append(tab + "对" + getName() + "进行查询杀毒\n");
		for (ConsistentComponent temp : list) {
			content.append(temp.doSomething(display, tab + "\t"));
		}
		return content.toString();
	}
	
}
